package com.ncloud.system.mapper;

import java.util.List;

import org.apache.ibatis.annotations.Delete;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Result;
import org.apache.ibatis.annotations.Results;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import com.alibaba.fastjson.JSONObject;
import com.ncloud.system.entity.ComputerRoom;

@Mapper
public interface ComputerRoomMapper{

	@Select("SELECT CONCAT(room_id,'') as room_id, room_name, CONCAT(area_id,'') as area_id, area_name, "
			+ "CONCAT(country_id,'') as country_id,country_name, "
			+ "CONCAT(province_id,'') as province_id,province_name, "
			+ "CONCAT(city_id,'') as city_id, city_name, addr, room_type, "
			+ "(select dict_value from sys_dictionary t1 where dictionary_id = 'room_type' and t1.dict_key=t.room_type) as room_type_name,"
			+ "room_state, contact_person, contact_phone, create_user,"
			+ "date_format(create_date, '%Y-%m-%d %H:%i:%s' ) as create_date, "
			+ "update_user,date_format(update_date, '%Y-%m-%d %H:%i:%s' ) as update_date, remarks FROM sys_computer_room t "
			+ "where (#{0}=0 or area_id = #{0}) and (#{1}=0 or country_id = #{1}) and (#{2}=0 or province_id = #{2}) and (#{3}=0 or city_id = #{3}) ")
	@Results({
		@Result(column="room_id",property="roomId"),@Result(column="room_name",property="roomName"),
		@Result(column="area_id",property="areaId"),@Result(column="area_name",property="areaName"),
		@Result(column="country_id",property="countryId"),@Result(column="country_name",property="countryName"),
		@Result(column="province_id",property="provinceId"),@Result(column="province_name",property="provinceName"),
		@Result(column="city_id",property="cityId"),@Result(column="city_name",property="cityName"),
		@Result(column="addr",property="addr"),@Result(column="room_type",property="roomType"),@Result(column="room_type_name",property="roomTypeName"),
		@Result(column="room_state",property="roomState"),@Result(column="contact_person",property="contactPerson"),
		@Result(column="contact_phone",property="contactPhone"),@Result(column="create_user",property="createUser"),
		@Result(column="create_date",property="createDate"),@Result(column="update_user",property="updateUser"),
		@Result(column="update_date",property="updateDate"),@Result(column="remarks",property="remarks")})
	List<JSONObject> getComputerRoomInfo(String areaId,String countryId,String provinceId,String cityId);
	
	@Insert(value = "INSERT INTO sys_computer_room (room_id, room_name, area_id, area_name, country_id,country_name, province_id,province_name, city_id, city_name, addr, room_type, room_state,"
			+ "contact_person, contact_phone, create_user, create_date, remarks) "
			+ "values(#{roomId},#{roomName},#{areaId},#{areaName},#{countryId},#{countryName},#{provinceId},#{provinceName},#{cityId},#{cityName},#{addr},#{roomType},#{roomState},"
			+ "#{contactPerson},#{contactPhone},#{createUser},#{createDate},#{remarks})")
    public int insertComputerRoom(ComputerRoom computerRoom);
	
	@Update(value = "UPDATE sys_computer_room SET room_name = #{roomName}, area_id = #{areaId}, area_name = #{areaName}, country_id = #{countryId},country_name = #{countryName}, "
			+ "province_id = #{provinceId}, province_name = #{provinceName}, city_id = #{cityId},city_name = #{cityName}, addr = #{addr}, room_type = #{roomType}, room_state = #{roomState}, "
			+ "contact_person = #{contactPerson}, contact_phone = #{contactPhone}, update_user = #{updateUser}, update_date = #{updateDate}, remarks = #{remarks} WHERE room_id = #{roomId}")
    public int updateComputerRoom(ComputerRoom computerRoom);
	
	@Delete(value = "delete from sys_computer_room WHERE room_id = #{roomId}")
    public int deleteComputerRoom(Integer roomId);
	
	@Select("select cabinet_id from res_cabinet where room_id=#{roomId} ")
	@Results({@Result(column="cabinet_id",property="cabinetId")})
	List<JSONObject> getCabinetByRoomId(Integer roomId);
}
